package com.yyaccp.appinfo.controller;

import com.yyaccp.appinfo.domain.DevUser;
import com.yyaccp.appinfo.exception.LoginExceptin;
import com.yyaccp.appinfo.service.DevUserService;
import com.yyaccp.appinfo.utils.AjaxResult;
import com.yyaccp.appinfo.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author 天天向上 （john.yi@qq.com）
 * @date 2020/11/20.
 */
@RestController
@RequestMapping("/api/devUsers")
public class DevUserController {
    @Autowired
    private DevUserService devUserService;

    @GetMapping("/test")
    public AjaxResult test(HttpSession session) {
        System.out.println(session.getId());
        return AjaxResult.success(session.getId());
    }
    @GetMapping("/getLoginUser")
    public AjaxResult getLoginUser(HttpSession session) {
        System.out.println("getLogin:" + session.getId());
        DevUser user = (DevUser) session.getAttribute("loginUser");
        if (user == null) {
            return AjaxResult.error();
        }
        System.out.println(user);
        System.out.println("hello world");
        System.out.println("世界你好");
        System.out.println("hello world");
        System.out.println("世界你好");
        return AjaxResult.success(user);
    }

    @PostMapping("/login")
    public AjaxResult login(DevUser user, HttpSession session) {
        try {
            DevUser devUser = devUserService.login(user);
            devUser.setDevPassword("******");
            session.setAttribute(Constants.LOGIN_USER, devUser);
            System.out.println("login:"+session.getId());
            return AjaxResult.success(devUser);
        } catch (LoginExceptin ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }
}
